<?php
namespace Home\Controller;
use Common\Controller\CommonController;

class IndexController extends CommonController {
	
	function _initialize(){
		parent::_initialize();
		$LG_USER = session('LG_USER');
		if(empty($LG_USER)){
        	$this->error('请先登录', U('Public/login')); 
        } 
		$this->assign('LG_USER',session('LG_USER')); 
	}
	
	function index(){
		$this->display('index');
	}
	
	function page(){
		$this->display('page');
	}
	
	function test1(){
		exit('Test1');
	}
	function test2(){
		sleep(10);
		exit('Test2');
	}
	function test3(){
		exit('Test3');
	}
	function test4(){
		exit('Test4');
	}
	
	function form(){
		$this->display('form');
	}
	function datalist(){
		$this->display('datalist');
	}
	/* *
	 * Dragon
	 * 2016-11-25
	 * 轮播图首页
	 * */
	function img() {
		
		$art=M('index_img');
		$Count=$art->count();
		$Page = new \Think\Pager($Count,10);

		$show=$Page->showhtml();

		$res = M('index_img')->order('sort desc')->select();
		$list = array();
		foreach ($res as $key => $value) {
			$value['img'] = json_decode($value['img'], true);
			$value['img'] = $value['img'][0];
			$list[] = $value;
		}
		$this->assign('page',$show);
		$this -> assign('list', $list);
		$this -> display('img');

	}

	// 编辑首页轮播
	public function img_edit($value='')
	{
		$id = I('id');
        if(IS_POST){
			$title = I('title');
			$iocn = I("iocn");
			if(empty($title)){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入标题'));
			}
			if(empty($iocn)){
                $this->ajaxReturn(array('s'=>false,'m'=>'请上传图片'));
			}
			
			$url = I("src");
			$iocn = $iocn ? [$iocn] : [];
			$img = json_encode($iocn);
			$sort = I("sort/d", 0);
			$data = ['url' => $url, 'img' => $img, 'title'=>$title];
			$data['sort'] = $sort;
            $data['content'] = I('content') ? html_entity_decode(I('content')) : '';
            $model = M('index_img');

            $res = $model->where(['id'=>$id])->save($data);
            if($res!==false){
                $this->ajaxReturn(array('s'=>true,'m'=>'修改成功', 'url'=>U('img')));
            }else{
                $this->ajaxReturn(array('s'=>false,'m'=>'修改失败!'));
            }
        }else{
            $info = M('index_img')->where(['id'=>$id])->find();
            $this->assign('info', $info);
            $this->display();
        }		
	}

	/* *
	 * Dragon
	 * 2016-11-25
	 * 添加轮播图
	 * */
	function add_advertising() {
		if(IS_POST){
			$title = I('title');
			$iocn = I("iocn");
			if(empty($title)){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入标题'));
			}
			if(empty($iocn)){
                $this->ajaxReturn(array('s'=>false,'m'=>'请上传图片'));
			}
			$url = I("src");
			$iocn = $iocn ? [$iocn] : [];
			$img = json_encode($iocn);
			$sort = I("sort/d", 0);
			$data = ['url' => $url, 'img' => $img, 'title'=>$title];
			$data['sort'] = $sort;
            $data['content'] = I('content') ? html_entity_decode(I('content')) : '';
			$res = M('index_img') -> add($data);
			if ($res) {
				$this -> ajaxReturn(array('s' => true, 'm' => '添加成功', 'url'=>U('img')));
			} else {
				$this -> ajaxReturn(array('s' => false, 'm' => '添加失败'));
			}
		}else{
            $this->display();
		}
	}
	
		/* *
	 * Dragon
	 * 2016-11-25
	 * 删除轮播图
	 * */
	function del_advertising() {
		$where['id'] = I("id");
		$res = M('index_img') -> where($where) -> delete();
		if ($res) {
			$this -> redirect('Index/img');
		}
	
	}
	
	/*
	 * lzm
	 * 2017年1月17日11:08:18
	 * 版本控制 index
	*/
	public function version_index(){
		$list	=	M('version')->order('id asc')->select();
		foreach ($list as $key => $value) {
			$list[$key]['ctime']=date('Y-m-d H:i:s',$value['ctime']);
		}
		$this->assign('list',$list);
		$this->display('version_index');
	} 
	/*
	 * lzm
	 * 2017年1月17日11:08:18
	 * 版本控制 add
	*/
	public function version_add(){
		$version	=	I('version');
		$system	=	I('system');
		$url		=	I('url');
		$content	=	I('content');
		$data		=	array('version'=>$version,'url'=>$url,'system'=>$system,'content'=>$content,'ctime'=>time());
		$res		=	M('version')->add($data);
		if(false!==$res){
			$this->ajaxReturn(array('s'=>true,'m'=>"添加成功"));
		}else{
			$this->ajaxReturn(array('s'=>false,'m'=>"添加失败"));
		}	
	} 
	
	
	
	
}